package Aleehot100;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * ClassName: c086
 * Package: Aleehot100
 * Description:139. 单词拆分
 *
 * @Author BCXJ
 * @Create 2025/9/9 23:07
 * @Version 1.0
 * @Since 1.0
 */
public class c086 {

    public static void main(String[] args) {
        ArrayList<String> objects = new ArrayList<>();
        objects.add("123");
        objects.add("123");
        objects.add("1234");
        HashSet<String> set = new HashSet<>(objects);
        System.out.println(set);
    }

    //
    public boolean wordBreak(String s, List<String> wordDict) {
        // 提高查找效率
        HashSet<String> index = new HashSet<>(wordDict);
        int n = s.length();

        // dp[i] 前i个字符能否被拼接
        boolean[] dp = new boolean[n + 1];
        dp[0] = true; // 前0个, 也就是""

        for (int i = 1; i <= n; i++) {
            // 检查所有的分割点
            for (int j = 0; j < i; j++) {
                if(dp[j] && index.contains(s.substring(j, i))) {
                    dp[i] = true;
                    break;
                }
            }
        }
        return dp[n];
    }

}
